<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title></title>
		<meta content=" " name="keywords">
			<meta content=" " name="description">
				<%@ include file="/jsp/front/common/header.jsp"%>
	</head>
	<body>
		<%@ include file="/jsp/front/common/nav.jsp"%>

		<div id="MainWrap">
			<div id="cart-index" class="CartWrap">
				<div class="CartNav clearfix">
					<div class="floatLeft">
						<img alt="购物流程--查看购物车" src="<c:url value="/images/statics/cartnav-step1.gif"/>" />
					</div>
					<div class="floatRight">
						<img src="<c:url value="/images/statics/cartnav-cart.gif"/>">
					</div>
				</div>
				<div id="cart-items" class="section">
					<form method="get" action="member_order_add.html" id="form-cart">
						<div id="cartItems" class="FormWrap">
							<h3>
								商品:
							</h3>
							<div class="division" id="goodsbody">
								<table width="100%" cellspacing="0" cellpadding="3" class="liststyle">
									<thead>
										<tr>
											<th class="span-2">
												图片
											</th>
											<th>
												商品名称
											</th>
											<th class="span-2">
												商品积分
											</th>
											<th class="span-2">
												销售价格
											</th>
											<th class="span-2">
												优惠价格
											</th>
											<th class="span-2">
												数量
											</th>
											<th class="span-2">
												小计
											</th>
											<th class="span-2">
												删除
											</th>
										</tr>
									</thead>
									<c:forEach items="${cartProductItems}" var="item" varStatus="index">
										<tr>
											<td>
												<div style="width: 50px; height: 50px;" class="cart-product-img">
													<a href="dp_goods_get.html?goodsId=${item.goodsId}" target="_blank" style="border: 0px none;"> <img width="41" height="50" src="${item.thumbnailPic}" style="cursor: pointer;" /> </a>
												</div>
											</td>
											<td style="text-align: left;">
												<a target="_blank" href="dp_goods_get.html?goodsId=${item.goodsId}">${item.name} ${item.pdtDesc}</a>
											</td>
											<td>
												${item.point}
											</td>
											<td class=" mktprice1">
												<fmt:message key="common_currency" />
												<fmt:formatNumber pattern="0.00" value="${item.mktPrice}" />
											</td>
											<td>
												<fmt:message key="common_currency" />${item.price}
												<fmt:formatNumber pattern="0.00" value="${item.discountPrice}" />
											</td>
											<td>
												<c:if test="${item.cartNum !=0}">
													<div class="Numinput">
														<input id="item_p_${index.index}" size="3" readonly="readonly" calculate="1" item="${item.productId}" itemtype="goods" itemprice="${item.discountPrice}"
															itemmax="${item.store-item.freez}" value="${item.cartNum}" class="_x_ipt textcenter" />
														<span class="numadjust increase" onclick="updateCartItem('${index.index}',1)"></span>
														<span class="numadjust decrease" onclick="updateCartItem('${index.index}',-1)"></span>
													</div>
												</c:if>
												<c:if test="${item.cartNum==0}">库存不足	</c:if>
											</td>
											<td class="itemTotal fontcolorRed" id="item_p_toal_${index.index}">
												<fmt:message key="common_currency" />
												<fmt:formatNumber pattern="0.00" value="${item.discountPrice * item.cartNum}" />
											</td>

											<td>
												<span onclick="removeCartItem(this)" item="${item.productId}" itemtype="goods" class="lnk quiet fontcolorRed"><img
														style="width: 13px; height: 13px; background-image: url(&quot;<c:url value="/images/statics/bundle.gif"/>&quot;); background-repeat: no-repeat; background-position: 0pt -27px;" alt="删除"
														src="<c:url value="/images/statics/transparent.gif"/>" /> </span>
											</td>
										</tr>
									</c:forEach>
								</table>
							</div>
							<h3>
								捆绑商品:
							</h3>
							<div class="division" id="goodsbody">
								<table width="100%" cellspacing="0" cellpadding="3" class="liststyle">
									<thead>
										<tr>

											<th>
												商品名称
											</th>
											<th class="span-2">
												优惠价格
											</th>
											<th class="span-2">
												数量
											</th>
											<th class="span-2">
												小计
											</th>
											<th class="span-2">
												删除
											</th>
										</tr>
									</thead>
									<c:forEach items="${goodsPackages}" var="item" varStatus="index">
										<tr>

											<td style="text-align: left;">
												 ${item.name}  
											</td>
											<td>
												<fmt:message key="common_currency" />
												<fmt:formatNumber pattern="0.00" value="${item.packageDiscount}" />
											</td>
											<td>
												<c:if test="${item.cartNum !=0}">
													<div class="Numinput">
														<input id="item_package_${index.index}" size="3" readonly="readonly" calculate="1" item="${item.gpackageId}" itemtype="package" itemprice="${item.packageDiscount}"
															itemmax="${item.packageStore - item.buyCount}" value="${item.cartNum}" class="_x_ipt textcenter" />
														<span class="numadjust increase" onclick="updatePackageCartItem('${index.index}',1)"></span>
														<span class="numadjust decrease" onclick="updatePackageCartItem('${index.index}',-1)"></span>
													</div>
												</c:if>
												<c:if test="${item.cartNum==0}">库存不足	</c:if>
											</td>
											<td class="itemTotal fontcolorRed" id="item_package_toal_${index.index}">
												<fmt:message key="common_currency" />
												<fmt:formatNumber pattern="0.00" value="${item.packageDiscount * item.cartNum}" />
											</td>

											<td>
												<span onclick="removePackageCartItem(this)" item="${item.gpackageId}" itemtype="package" class="lnk quiet fontcolorRed"><img
														style="width: 13px; height: 13px; background-image: url(&quot;<c:url value="/images/statics/bundle.gif"/>&quot;); background-repeat: no-repeat; background-position: 0pt -27px;" alt="删除"
														src="<c:url value="/images/statics/transparent.gif"/>" /> </span>
											</td>
										</tr>
									</c:forEach>
								</table>
							</div>

							<h3>
								优惠活动:
							</h3>
							<div class="division">
								<table width="100%" cellspacing="0" cellpadding="3" class="liststyle">
									<thead>
										<tr>

											<th class="span-4">
												优惠名称
											</th>
											<th>
												优惠时间
											</th>
											<th class="span-2">
												优惠方式
											</th>
											<th class="span-4">
												优惠内容
											</th>
											<th class="span-5">
												优惠条件
											</th>
											<th class="span-2">
												当前可用
											</th>
										</tr>
									</thead>
									 
									<c:forEach items="${marketPromotions}" var="promotion" varStatus="index">
										<tr>

											<td style="text-align: left;">
												${promotion.pmtDescribe}
											</td>
											<td>
												${promotion.pmtTimeBeginString}---${promotion.pmtTimeEndString}
											</td>
											<td>
												${promotion.pmtDesc}
											</td>
											<td style="text-align: left;">
												${promotion.pmtContent}
											</td>
											<td>
												<fmt:formatNumber pattern="0.00" value="${promotion.orderMoneyFrom }" />
												--
												<fmt:formatNumber pattern="0.00" value="${promotion.orderMoneyTo }" />
											</td>
											<td>
												<input type="hidden"  pmtIfcoupon="${promotion.pmtIfcoupon}" from="${promotion.orderMoneyFrom}" to="${promotion.orderMoneyTo}"
													discount="${promotion.discount}" dctype="${promotion.discountMethod}" pmt='pmt' pmtId="${promotion.pmtId}" />
												<input size="6" readonly="readonly" id="pmt_used_${promotion.pmtId}" style="border: 1px #ffffff;" />
											</td>
										</tr>
									</c:forEach>
								</table>
							</div>
							<h3>
								使用优惠券:<a href="javascript:void(0)" onclick="clearCoupon()">删除该优惠券</a>
							</h3>
							<div class="division">
								<table width="100%" cellspacing="0" cellpadding="3" class="liststyle">
									<thead>
										<tr>

											<th>
												请输入优惠券号码
											</th>
											<th class="span-9">
												优惠券编号
											</th>
										</tr>
									</thead>
									<tr>

										<td style="text-align: left;">
											<input size="50" id="s-ipt-coupon" class="x-input " />
											<input onclick="checkCoupon()" type="button" value="确定" <c:if test="${not empty memberCoupon }">disabled="disabled"</c:if> id="btn_check_coupon" />
											<input type="hidden" id="order_member_coupon" value="${memberCoupon.mcId}" from="${memberCoupon.marketPromotion.orderMoneyFrom}" to="${memberCoupon.marketPromotion.orderMoneyTo}"
												discount="${memberCoupon.marketPromotion.pmtMethodValue}" dctype="${memberCoupon.marketPromotion.pmtMethod}" />
										</td>
										<td id="coupon_code">
											${memberCoupon.mcCode}
										</td>
									</tr>
								</table>
							</div>
							<h3>
								订单金额:
							</h3>
							<div class="division">
								<table width="100%" cellspacing="0" cellpadding="3" class="liststyle">
									
									<tr>

										<td style="text-align: right;" colspan="4">
											原始商品总额<fmt:message key="common_currency" />：
										</td>
										<td colspan="2">
											<span style="color: rgb(255, 0, 0);" class="mktprice1" id="total_raw_price"></span>
										</td>

									</tr>
									<tr>

										<td style="text-align: right;" colspan="4">
											商品总额<fmt:message key="common_currency" />：
										</td>
										<td colspan="2">
											<span class="totalprice price1" id="total_discount_price"></span>
										</td>

									</tr>
								</table>
							</div>
							<h3>
								积分兑换赠品
							</h3>
							<div class="division" id="giftbody">
								<table width="100%" cellspacing="0" cellpadding="0" class="liststyle">
									<thead>
										<tr>
											<th class="span-2">
												图片
											</th>
											<th>
												赠品名称
											</th>
											<th class="span-2">
												所需积分
											</th>
											<th class="span-2">
												限购数量
											</th>
											<th class="span-1">
												数量
											</th>
											<th class="span-2 ColColorOrange">
												小计
											</th>
											<th class="span-2">
												删除
											</th>
										</tr>
									</thead>
									<c:forEach items="${marketGifts}" var="gift" varStatus="index">
										<tr>
											<td>
												<a style="margin: 0pt; padding: 0pt; text-decoration: none;" target="_blank" href="dp_gift_get.html?giftId=${gift.giftId}">
													<img width="120" height="120" src="${gift.thumbnailPic}" />
												</a>
											</td>
											<td style="text-align: left;">
												<a target="_blank" href="dp_gift_get.html?giftId=${gift.giftId}">${gift.name}</a>
											</td>
											<td>
												${gift.point}
											</td>
											<td>
												${gift.limitNum}
											</td>
											<td>
												<div class="Numinput">
													<input type="text" readonly="readonly" size="3" value="${gift.cartNum}" id="item_gift_${index.index}" item="${gift.giftId}" itemtype="gift" itempoint="${gift.point}"
														itemmax="${gift.storage-gift.freez}" class="_x_ipt textcenter">
														<span onclick="updateGiftCartItem('${index.index}',1)" class="numadjust increase"></span> <span onclick="updateGiftCartItem('${index.index}',-1)" class="numadjust decrease"></span>
												</div>
											</td>
											<td class="itemTotal" id="item_gift_toal_${index.index}">
												${gift.cartNum * gift.point}
											</td>
											<td>
												<span onclick="removeGiftCartItem(this)" item="${gift.giftId}" itemtype="gift" class="lnk quiet fontcolorRed"><img
														style="width: 13px; height: 13px; background-image: url(&quot;<c:url value="/images/statics/bundle.gif"/>&quot;); background-repeat: no-repeat; background-position: 0pt -27px;" alt="删除"
														src="<c:url value="/images/statics/transparent.gif"/>" /> </span>
											</td>
										</tr>
									</c:forEach>
								</table>
							</div>

							<div style="width: 85%;" class="floatRight">
								<div id="cartTotal">
								</div>
							</div>
							<div class="clear"></div>
						</div>
						<div style="padding: 10px 0 25px 0 ;" class="CartBtn clearfix">
							<div class="span-auto">
								<a class="actbtn btn-return" href="<c:url value="/"/>">«继续购物</a>
							</div>
							<div class="span-auto">
								<input type="button" onclick="emptyCart();" value="清空购物车" class="actbtn btn-clearcat" />
							</div>
							<div class="span-auto floatRight">
								<input type="submit" value="下单结算»" class="actbtn btn-next" />
								<!--								<a class="actbtn btn-next" >下单结算»</a>-->
							</div>
						</div>
					</form>
				</div>
			</div>

		</div>



		<%@ include file="/jsp/front/common/footer.jsp"%>

		<script type="text/javascript" src="<c:url value='/scripts/jquery-1.4.2.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/scripts/cart.js'/>"></script>

		<script type="text/javascript">
			function addCoupon(id){
				 
				setCookie(cookie_coupon_name,id,mallDays);
			}
		
			function updateCartItem(objid,num){
				var obj = $('#item_p_'+objid);
				var curNum = obj.val();
				var newNum = num + parseInt(curNum);
				if(newNum <= 0){
					return;
				}
				var max = parseInt(obj.attr('itemmax'));
				if(newNum>max){
					return;
				}
				
				obj.val( newNum);
				var id = obj.attr('item');
				var itemtype = obj.attr('itemtype');
				
				updateItem(id,newNum,itemtype);
				
				var price = obj.attr('itemprice');
				$('#item_p_toal_'+objid).html('');
				$('#item_p_toal_'+objid).html(price*newNum);
				
				totalPrice();
			}
			
			function removeCartItem(obj){
				 if(!confirm('是否删除')){
				 	return;
				 }
				 $(obj).parent().parent().remove();
				var id = $(obj).attr('item');
				var itemtype = $(obj).attr('itemtype');				
				deleteItem(id,itemtype);
				totalPrice();
			}
			
			function updatePackageCartItem(objid,num){
				var obj = $('#item_package_'+objid);
				var curNum = obj.val();
				var newNum = num + parseInt(curNum);
				if(newNum <= 0){
					return;
				}
				var max = parseInt(obj.attr('itemmax'));
				if(newNum>max){
					return;
				}
				
				obj.val( newNum);
				var id = obj.attr('item').trim();
				var itemtype = obj.attr('itemtype');
				
				updateItem(id,newNum,itemtype);
				
				var price = obj.attr('itemprice');
				$('#item_package_toal_'+objid).html('');
				$('#item_package_toal_'+objid).html(price*newNum);
				
				totalPrice();
			}
			var member_available_point =${members.point}-${members.pointFreeze} ;
			function removePackageCartItem(obj){
				 if(!confirm('是否删除')){
				 	return;
				 }
				 $(obj).parent().parent().remove();
				var id = $(obj).attr('item');
				var itemtype = $(obj).attr('itemtype');				
				deleteItem(id,itemtype);
				
				
				totalPrice();
			}
			
			function updateGiftCartItem(giftid,num){
				var obj = $('#item_gift_'+giftid);
				var curNum = obj.val();
				var newNum = num + parseInt(curNum);
				if(newNum <= 0){
					return;
				}
				var max = parseInt(obj.attr('itemmax'));
				if(newNum>max){
					return;
				}
				
				obj.val( newNum);
				var id = obj.attr('item').trim();
				var itemtype = obj.attr('itemtype');
				
				updateItem(id,newNum,itemtype);
				
				var price = obj.attr('itempoint');
				$('#item_gift_toal_'+giftid).html('');
				$('#item_gift_toal_'+giftid).html(price*newNum);
			}
			
			function removeGiftCartItem(obj){
				 if(!confirm('是否删除')){
				 	return;
				 }
				 $(obj).parent().parent().remove();
				var id = $(obj).attr('item');
				var itemtype = $(obj).attr('itemtype');				
				deleteItem(id,itemtype);
				
			}
			
			function totalPrice(){
				var total_price=0;
				$("input[calculate='1']").each(function(i, item) {		  
					 var num = $(item).val();
					 var itemprice = $(item).attr('itemprice');
					 total_price += num*itemprice;
				});
				$('#total_raw_price').html('');
				$('#total_raw_price').html(parseFloat(total_price).toFixed(2));
				var discountTotal=  total_price;
				$("input[pmt='pmt']").each(function(i, pmt) {		  
					 var from = $(pmt).attr('from');
					 var to = $(pmt).attr('to');
					 var discount =  $(pmt).attr('discount');
					 var dctype = $(pmt).attr('dctype');
					 var pmtId =  $(pmt).attr('pmtId');
					 var pmtIfcoupon = $(pmt).attr('pmtIfcoupon');
					 if(to==0 || total_price>from && total_price<to){
					 	$(pmt).val(pmtId);					 	
					 	if(dctype == 'sub'){
					 		discountTotal = discountTotal -discount ;
					 	}else{
					 		discountTotal = discount *discountTotal ;
					 	}
					 	$('#pmt_used_'+pmtId).val('满足');
					 }else{
					 	$(pmt).val('');
					 	$('#pmt_used_'+pmtId).val('不满足');
					 }
				});
				
				var coupon = $('#order_member_coupon').attr('value');
				
				if(coupon !=''){
					var from = $('#order_member_coupon').attr('from');
					var to = $('#order_member_coupon').attr('to');
					var dctype = $('#order_member_coupon').attr('dctype');
					var discount = $('#order_member_coupon').attr('discount');
					
					if(dctype =='coupon_cash' || dctype =='coupon_less' ){
						discountTotal = discountTotal -discount ;
					}else{
						discountTotal = discount *discountTotal ;
					}
				}
				
				var total_discount_price =  parseFloat(discountTotal).toFixed(2);
				$('#total_discount_price').html('');
				$('#total_discount_price').html(total_discount_price);			
				
			}
			
			totalPrice();
			
			<c:forEach items="${cartProductItems}" var="item" varStatus="index">
					<c:if test="${item.cartNum <= 0}">deleteItem(${item.productId},'goods');</c:if>
					<c:if test="${item.cartNum > 0}"> 
					updateItem(${item.productId},${item.cartNum},'goods');
					</c:if>
			</c:forEach>
			
			<c:forEach items="${goodsPackages}" var="item" varStatus="index">
					<c:if test="${item.cartNum <= 0}">deleteItem(${item.gpackageId},'package');</c:if>
					<c:if test="${item.cartNum > 0}"> 
					updateItem(${item.gpackageId},${item.cartNum},'package');
					</c:if>
			</c:forEach>
			
			<c:forEach items="${marketGifts}" var="item" varStatus="index">
					<c:if test="${item.cartNum <= 0}">deleteItem(${item.giftId},'gift');</c:if>
					<c:if test="${item.cartNum > 0}"> 
					updateItem(${item.giftId},${item.cartNum},'gift');
					</c:if>
			</c:forEach>
			
			function isPmtIfcoupon(){
				$("input[pmt='pmt']").each(function(i, pmt) {						 
					 var pmtIfcoupon = $(pmt).attr('pmtIfcoupon');
					 if(!pmtIfcoupon){
					 	return false;
					 }
				});
				return true;
			}
			
			function checkCoupon(){
				var couponCode = $('#s-ipt-coupon').val();
				var pmtIfcoupon = isPmtIfcoupon() ;
				if(couponCode!=''){
					var url = "member_coupon_check.html";
					var params = {
						"couponCode" : couponCode,
						"pmtIfcoupon": pmtIfcoupon
					};
			
					$.getJSON(url, params, 
						function callback(data) 
						{
							var memberCoupon = data.memberCoupon;
							if(memberCoupon == null){
								alert(data.reason);
							}else{
								addCoupon(memberCoupon.mcId);
								$('#coupon_code').html('');
								$('#coupon_code').html(couponCode);
								$('#order_member_coupon').attr('value',memberCoupon.mcId);
								$('#order_member_coupon').attr('from',memberCoupon.marketPromotion.orderMoneyFrom);
								$('#order_member_coupon').attr('to',memberCoupon.marketPromotion.orderMoneyTo);
								$('#order_member_coupon').attr('dctype',memberCoupon.marketPromotion.pmtMethod);
								$('#order_member_coupon').attr('discount',memberCoupon.marketPromotion.pmtMethodValue);
								
								$('#btn_check_coupon').attr('disabled',true);
								
								totalPrice();
								
							}
							
						}
					);
					
				}else{
					 
				}
				
				
			}
			
			function clearCoupon(){
				$('#order_member_coupon').attr('value','');
				deleteCoupon();
				$('#coupon_code').html('');
				totalPrice();
				$('#btn_check_coupon').attr('disabled',false);
			}
			 
		</script>
	</body>
</html>